home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Shutdown FX 1.5 source Folder / Shutdown FX ƒ / code ƒ / ◊ main.c < prev    next >
Encoding:
Text File  |  1994-04-20  |  11.4 KB  |  370 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        main.c
  4.  
  5. Purpose:    This module handles the actual shutdown proc -- creating
  6.             a grafport on the screen and clearing it (dispatching
  7.             the graphic effects).
  8.             
  9.  
  10. Shutdown FX -=- graphic effects on shutdown
  11. Copyright (C) 1993-4, Mark Pilgrim & Dave Blumenthal
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "main.h"
  31. #include "init.h"
  32. #include "fade headers.h"
  33. #include "reversed fade headers.h"
  34. #include "globals.h"
  35. #include "prefs.h"
  36. #include "Shutdown.h"
  37.  
  38. #define NUM_WIPES 50        /* wow */
  39.  
  40. Handle            gTheDitl;
  41. DialogTHndl        gTheDlog;
  42. unsigned char    gOnShutdown, gOnRestart, gSequential, gIsVirgin;
  43. int                gWhichWipe;
  44.  
  45. void sfxShutdownMain(void)
  46. {
  47.     unsigned int    theKeys[8];
  48.     int                resultCode;
  49.     Boolean            oldRestart;
  50.     
  51.     SetUpA4();
  52.     
  53.     oldRestart=gOnRestart;
  54.     GetKeys(&theKeys);
  55.     if ((gIsVirgin) || (theKeys[3]&4))    /* first time, or option key held down */
  56.     {
  57.         resultCode=DoOptionsDialog(FALSE);
  58.         if (resultCode==1)    /* shutdown */
  59.         {
  60.             if (gOnShutdown)
  61.                 DoTheDangFade();
  62.         }
  63.         else if (resultCode==2)    /* restart */
  64.         {
  65.             if (gOnRestart)
  66.                 DoTheDangFade();
  67.             if (oldRestart)        /* take our restart proc out of queue if it's there */
  68.                 ShutDwnRemove((ProcPtr)sfxRestartMain);
  69.             RestoreA4();
  70.             ShutDwnStart();        /* initiate restart */
  71.             ExitToShell();
  72.         }
  73.     }
  74.     else if (gOnShutdown)
  75.         DoTheDangFade();
  76.     
  77.     RestoreA4();
  78. }
  79.  
  80. void sfxRestartMain(void)
  81. {
  82.     unsigned int    theKeys[8];
  83.     int                resultCode;
  84.     Boolean            oldShutdown;
  85.     
  86.     SetUpA4();
  87.     
  88.     oldShutdown=gOnShutdown;
  89.     GetKeys(&theKeys);
  90.     if ((gIsVirgin) || (theKeys[3]&4))    /* first time, or option key held down */
  91.     {
  92.         resultCode=DoOptionsDialog(TRUE);
  93.         if (resultCode==2)    /* shutdown */
  94.         {
  95.             if (gOnShutdown)
  96.                 DoTheDangFade();
  97.             if (oldShutdown)    /* take our shutdown proc out of queue if it's there */
  98.                 ShutDwnRemove((ProcPtr)sfxShutdownMain);
  99.             RestoreA4();
  100.             ShutDwnPower();        /* initiate shutdown */
  101.             ExitToShell();
  102.         }
  103.         else if (resultCode==1)    /* restart */
  104.         {
  105.             if (gOnRestart)
  106.                 DoTheDangFade();
  107.         }
  108.     }
  109.     else if (gOnRestart)
  110.         DoTheDangFade();
  111.     
  112.     RestoreA4();
  113. }
  114.  
  115. void DoTheDangFade(void)
  116. {
  117.     int                oldMenuBarHeight;
  118.     long            oldA5;
  119.     QDGlobals        qd;                /* our QD globals. */
  120.     GrafPort        gp;                /* our grafport. */
  121.     GrafPtr            savePort;
  122.     int                whichWipe;
  123.     THz                saveZone;
  124.     unsigned long    temp;
  125.     
  126.     if (gSequential)
  127.     {
  128.         whichWipe=gWhichWipe;
  129.         gWhichWipe++;
  130.         if (gWhichWipe>=NUM_WIPES)
  131.             gWhichWipe=0;
  132.     }
  133.     else
  134.     {
  135.         GetDateTime(&temp);
  136.         whichWipe=(temp&0x7fffffff)%NUM_WIPES;
  137.         gWhichWipe=whichWipe;
  138.     }
  139.     SaveThePrefs();
  140.     
  141.     GetPort(&savePort);
  142.     oldMenuBarHeight=MBarHeight;
  143.     MBarHeight=0;
  144.     DrawMenuBar();
  145.  
  146.     /* get a value for A5, a structure that mirrors qd globals. */
  147.     oldA5 = SetA5((long)&qd.end);
  148.     InitGraf(&qd.thePort);
  149.     OpenPort(&gp);
  150.     
  151.     HideCursor();
  152.     
  153.     saveZone=GetZone();
  154.     SetZone(SysZone);
  155.  
  156.     if (whichWipe==0)        BoxInFade(gp.portRect, &qd.black);
  157.     else if (whichWipe==1)    BoxOutFade(gp.portRect, &qd.black);
  158.     else if (whichWipe==2)    CircleBulgeFade(gp.portRect, &qd.black);
  159.     else if (whichWipe==3)    CircleInFade(gp.portRect, &qd.black);
  160.     else if (whichWipe==4)    CircleOutFade(gp.portRect, &qd.black);
  161.     else if (whichWipe==5)    CircleSerendipityFade(gp.portRect, &qd.black);
  162.     else if (whichWipe==6)    CircularFade(gp.portRect, &qd.black);
  163.     else if (whichWipe==7)    DiagonalFade(gp.portRect, &qd.black);
  164.     else if (whichWipe==8)    FourCornerFade(gp.portRect, &qd.black);
  165.     else if (whichWipe==9)    FullScrollLRFade(gp.portRect, &qd.black);
  166.     else if (whichWipe==10)    FullScrollUDFade(gp.portRect, &qd.black);
  167.     else if (whichWipe==11)    HalvesScrollFade(gp.portRect, &qd.black);
  168.     else if (whichWipe==12) HilbertFade(gp.portRect, &qd.black);
  169.     else if (whichWipe==13)    MrDoOutdoneFade(gp.portRect, &qd.black);
  170.     else if (whichWipe==14)    MrDoFade(gp.portRect, &qd.black);
  171.     else if (whichWipe==15)    PourScrollFade(gp.portRect, &qd.black);
  172.     else if (whichWipe==16)    RandomFade(gp.portRect, &qd.black);
  173.     else if (whichWipe==17)    RescueRaidersFade(gp.portRect, &qd.black);
  174.     else if (whichWipe==18)    SkipalineLRFade(gp.portRect, &qd.black);
  175.     else if (whichWipe==19)    SkipalineFade(gp.portRect, &qd.black);
  176.     else if (whichWipe==20)    SpiralGyraFade(gp.portRect, &qd.black);
  177.     else if (whichWipe==21)    CircularFadeReversed(gp.portRect, &qd.black);
  178.     else if (whichWipe==22)    DiagonalFadeDownRight(gp.portRect, &qd.black);
  179.     else if (whichWipe==23)    FourCornerFadeReversed(gp.portRect, &qd.black);
  180.     else if (whichWipe==24)    FullScrollLeftFade(gp.portRect, &qd.black);
  181.     else if (whichWipe==25)    FullScrollUpFade(gp.portRect, &qd.black);
  182.     else if (whichWipe==26)    HalvesScrollFadeReversed(gp.portRect, &qd.black);
  183.     else if (whichWipe==27)    HilbertFadeReversed(gp.portRect, &qd.black);
  184.     else if (whichWipe==28)    MrDoOutdoneFadeReversed(gp.portRect, &qd.black);
  185.     else if (whichWipe==29)    MrDoFadeReversed(gp.portRect, &qd.black);
  186.     else if (whichWipe==30)    PourScrollFadeReversed(gp.portRect, &qd.black);
  187.     else if (whichWipe==31)    RescueRaidersFadeReversed(gp.portRect, &qd.black);
  188.     else if (whichWipe==32)    HGRFade(gp.portRect, &qd.black);
  189.     else if (whichWipe==33)    HGR2Fade(gp.portRect, &qd.black);
  190.     else if (whichWipe==34)    QuadrantFade2(gp.portRect, &qd.black);
  191.     else if (whichWipe==35)    QuadrantFade(gp.portRect, &qd.black);
  192.     else if (whichWipe==36)    QuadrantScroll2Fade(gp.portRect, &qd.black);
  193.     else if (whichWipe==37)    QuadrantScrollFade(gp.portRect, &qd.black);
  194.     else if (whichWipe==38)    SkipalineLR2PassFade(gp.portRect, &qd.black);
  195.     else if (whichWipe==39)    Skipaline2PassFade(gp.portRect, &qd.black);
  196.     else if (whichWipe==40)    HGRFadeReversed(gp.portRect, &qd.black);
  197.     else if (whichWipe==41)    HGR2FadeReversed(gp.portRect, &qd.black);
  198.     else if (whichWipe==42)    QuadrantFade2Reversed(gp.portRect, &qd.black);
  199.     else if (whichWipe==43)    QuadrantFadeReversed(gp.portRect, &qd.black);
  200.     else if (whichWipe==44)    QuadrantScroll2FadeReversed(gp.portRect, &qd.black);
  201.     else if (whichWipe==45)    QuadrantScrollFadeReversed(gp.portRect, &qd.black);
  202.     else if (whichWipe==46)    SkipalineLR2PassFadeReversed(gp.portRect, &qd.black);
  203.     else if (whichWipe==47)    Skipaline2PassFadeReversed(gp.portRect, &qd.black);
  204.     else if (whichWipe==48) SlideFade(gp.portRect, &qd.black);
  205.     else if (whichWipe==49) SlideFadeReversed(gp.portRect, &qd.black);
  206.     
  207. // You would think a simple switch-case statement would suffice, but you would
  208. // be mistaken.  In fact, the analogous switch-case causes a crash on a Powerbook
  209. // 100.  This is most likely a THINK C bug; it is super-optimizing the switch-case
  210. // so much that it no longer works on a 68000 machine.  The series of if-elses
  211. // is less elegant to the programmer, but more elegant to the user, since it
  212. // doesn't crash.  (:
  213.  
  214.     SetZone(saveZone);
  215.     
  216.     MBarHeight=oldMenuBarHeight;
  217.     ShowCursor();
  218.     ObscureCursor();
  219.     
  220.     ClosePort(&gp);
  221.     SetA5(oldA5);
  222.     SetPort(savePort);
  223. }
  224.  
  225. int DoOptionsDialog(Boolean needToSwapButtons)
  226. {
  227.     DialogTPtr        dtmp;
  228.     DialogRecord    dlog;
  229.     Handle            newDitl, itemH;
  230.     Rect            *theRect, box;
  231.     int                left, top, item, itemType;
  232.     
  233.     InitGraf(&thePort);
  234.     SetCursor(&arrow);
  235.     
  236.     HLock(gTheDlog);
  237.     newDitl=gTheDitl;
  238.     HandToHand(&newDitl);
  239.  
  240.     theRect = (Rect*) *gTheDlog;
  241.     left = (screenBits.bounds.right - (theRect->right - theRect->left)) / 2;
  242.     top = (screenBits.bounds.bottom - (theRect->bottom - theRect->top)) / 3;
  243.     
  244.     if(top < (GetMBarHeight() + 1))
  245.         top = GetMBarHeight() + 1;
  246.     theRect->right += left - theRect->left;
  247.     theRect->left = left;
  248.     theRect->bottom += top - theRect->top;
  249.     theRect->top = top;
  250.  
  251.     dtmp=*gTheDlog;
  252.     NewDialog(&dlog, theRect, dtmp->title, dtmp->visible, dtmp->procID,
  253.                 (WindowPtr)-1L, dtmp->goAwayFlag, dtmp->refCon, newDitl);
  254.     
  255.     GetDItem(&dlog, 12, &itemType, &itemH, &box);
  256.     InsetRect(&box, -4, -4);
  257.     SetDItem(&dlog, 12, itemType, (Handle)OutlineDefaultButton, &box);
  258.     GetDItem(&dlog, 6, &itemType, &itemH, &box);
  259.     SetCtlValue((ControlHandle)itemH, gOnRestart ? 1 : 0);
  260.     GetDItem(&dlog, 7, &itemType, &itemH, &box);
  261.     SetCtlValue((ControlHandle)itemH, gOnShutdown ? 1 : 0);
  262.     GetDItem(&dlog, 9, &itemType, &itemH, &box);
  263.     SetCtlValue((ControlHandle)itemH, gSequential ? 1 : 0);
  264.     GetDItem(&dlog, 10, &itemType, &itemH, &box);
  265.     SetCtlValue((ControlHandle)itemH, gSequential ? 0 : 1);
  266.     if (needToSwapButtons)
  267.     {
  268.         GetDItem(&dlog, 1, &itemType, &itemH, &box);
  269.         SetCTitle((ControlHandle)itemH, "\pRestart");
  270.         GetDItem(&dlog, 2, &itemType, &itemH, &box);
  271.         SetCTitle((ControlHandle)itemH, "\pShutdown");
  272.     }
  273.     
  274.     ShowWindow(&dlog);
  275.     
  276.     item=0;
  277.     do
  278.     {
  279.         ModalDialog((ProcPtr)ProcOFilter, &item);
  280.         switch (item)
  281.         {
  282.             case 6:        gOnRestart=(gOnRestart==0xFF) ? 0x00 : 0xFF;
  283.                         GetDItem(&dlog, 6, &itemType, &itemH, &box);
  284.                         SetCtlValue((ControlHandle)itemH, gOnRestart ? 1 : 0);
  285.                         if ((!gOnRestart) && (!gOnShutdown))
  286.                         {
  287.                             gOnShutdown=0xFF;
  288.                             GetDItem(&dlog, 7, &itemType, &itemH, &box);
  289.                             SetCtlValue((ControlHandle)itemH, 1);
  290.                         }
  291.                         break;
  292.             case 7:        gOnShutdown=(gOnShutdown==0xFF) ? 0x00 : 0xFF;
  293.                         GetDItem(&dlog, 7, &itemType, &itemH, &box);
  294.                         SetCtlValue((ControlHandle)itemH, gOnShutdown ? 1 : 0);
  295.                         if ((!gOnRestart) && (!gOnShutdown))
  296.                         {
  297.                             gOnRestart=0xFF;
  298.                             GetDItem(&dlog, 6, &itemType, &itemH, &box);
  299.                             SetCtlValue((ControlHandle)itemH, 1);
  300.                         }
  301.                         break;
  302.             case 9:        gSequential=0xFF;
  303.                         GetDItem(&dlog, 9, &itemType, &itemH, &box);
  304.                         SetCtlValue((ControlHandle)itemH, 1);
  305.                         GetDItem(&dlog, 10, &itemType, &itemH, &box);
  306.                         SetCtlValue((ControlHandle)itemH, 0);
  307.                         break;
  308.             case 10:    gSequential=0x00;
  309.                         GetDItem(&dlog, 10, &itemType, &itemH, &box);
  310.                         SetCtlValue((ControlHandle)itemH, 1);
  311.                         GetDItem(&dlog, 9, &itemType, &itemH, &box);
  312.                         SetCtlValue((ControlHandle)itemH, 0);
  313.                         break;
  314.         }
  315.     }
  316.     while ((item!=1) && (item!=2));
  317.     
  318.     gIsVirgin=0x00;
  319.     SaveThePrefs();
  320.     
  321.     HideWindow(&dlog);
  322.     CloseDialog(&dlog);
  323.     
  324.     HUnlock(gTheDlog);
  325.     DisposeHandle(newDitl);
  326.     
  327.     return item;
  328. }
  329.  
  330. pascal Boolean ProcOFilter(DialogPtr theDialog, EventRecord *theEvent, short *theItem)
  331. {
  332.     unsigned char    theChar;
  333.     short            itemType;
  334.     Handle            itemH;
  335.     Rect            box;
  336.     unsigned long    dummy;
  337.     
  338.     switch (theEvent->what)
  339.     {
  340.         case keyDown:
  341.         case autoKey:
  342.             theChar=theEvent->message & charCodeMask;
  343.             if ((theChar==0x0d) || (theChar==0x03))        /* RETURN or ENTER */
  344.             {
  345.                 *theItem=1;        /* as if the user selected item #1 */
  346.                 GetDItem(theDialog, 1, &itemType, &itemH, &box);
  347.                 HiliteControl((ControlHandle)itemH, 1);    /* flash button 1 by highlighting, */
  348.                 Delay(8, &dummy);                        /* waiting 8 ticks, and then */
  349.                 HiliteControl((ControlHandle)itemH, 0);    /* unhighlighting -- believe */
  350.                 return TRUE;                            /* it or not, that's Apple's */
  351.             }                                            /* preferred method */
  352.             break;
  353.     }
  354.     
  355.     return FALSE;
  356. }
  357.  
  358. pascal void OutlineDefaultButton(DialogPtr myDlog, short itemNum)
  359. {
  360.     short            itemType;
  361.     Handle            itemH;
  362.     Rect            box;
  363.     
  364.     GetDItem(myDlog, 1, &itemType, &itemH, &box);
  365.     PenSize(3, 3);
  366.     InsetRect(&box, -4, -4);
  367.     FrameRoundRect(&box, 16, 16);
  368.     PenNormal();
  369. }
  370.